/************************************************************************
This file is part of Polar4linux
Copyright (C) 2010-2011 Eric jrdn2
Portions Copyright (C) 2007 Matti Juvonen <mpj@iki.fi> (RS400 Tools)
Portions Copyright (C) 2006 Jani Hursti <jani.hursti@hut.fi>,
Portions Copyright (C) 2001-2003 Tom Oliveira e Silva

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <vector>
#include "polarSubs.h"

//loadFile searches for existing data in memory.
//if it finds them, it returns them
//else it read the file on the disk
//if the data is invalid, hrm_data.has_changed=-1;
//if everything is ok, hrm_data.has_changed=0;
hrm_data hrDealer::loadFile(std::string const &path,QDate const &thedate,unsigned int exerciseNumber)
{
	std::string hrmName = path+buildHRMfilename(thedate.year()-2000,thedate.month(),thedate.day(),exerciseNumber);
	std::string pddName = path+buildPDDfilename(thedate.year(),thedate.month(),thedate.day());

	bool found=false;
	rs400_session *s=NULL;
	for (unsigned int i=0;i<hrms.size();i++)
	{
		if (hrms[i]->filename==hrmName) {found=true;s=hrms[i];break;}
	}
	
	rs400_pdd_content *p=NULL;
	if (s!=NULL) //we found an opened exercise
	{
		for (unsigned int i=0;i<pdds.size();i++)
		{
			if (pdds[i]->filename==pddName) {p=pdds[i];break;}
		}
		//if pdd is not found and hrm is, really really bad things happen
	}
	else
	{
		s=new rs400_session;
		p=new rs400_pdd_content;
	}

	hrm_data ret;
	ret.session=s;
	ret.pddContent=p;
	ret.has_changed=ret.loadDATA(path,thedate,exerciseNumber);
	if (ret.has_changed==-1) {delete ret.session; delete ret.pddContent;}
	else {if (found==false) {hrms.push_back(s);pdds.push_back(p);}}
	return ret;
}

void hrDealer::clearData()
{
	if (hrms.size()!=0)
	{
		std::vector<rs400_session*>::iterator it1=hrms.end()-1;
		for (;it1>=hrms.begin();it1--)
		{
			if ((*it1)->refCount==0) {delete *it1;hrms.erase(it1);}
		}
	}

	if (pdds.size()!=0)
	{
		std::vector<rs400_pdd_content*>::iterator it2=pdds.end()-1;
		for (;it2>=pdds.begin();it2--)
		{
			if ((*it2)->refCount==0) {delete *it2;pdds.erase(it2);}
		}
	}
/*	
	for (unsigned int i=0;i<hrms.size();i++)delete hrms[i];
	for (unsigned int i=0;i<pdds.size();i++) delete pdds[i];
	
	hrms.clear();
	pdds.clear();
*/
}

hrDealer::~hrDealer()
{
	clearData();
	printState();
}

//debug function
void hrDealer::printState()
{
	std::cout<<"containers' sizes on dtor="<<hrms.size()<<" "<<pdds.size()<<std::endl;
	for (unsigned int i=0;i<hrms.size();i++) std::cout<<hrms[i]->refCount<<" ";
	std::cout<<std::endl;
	for (unsigned int i=0;i<pdds.size();i++) std::cout<<pdds[i]->refCount<<" ";
	std::cout<<std::endl;
}

polar4linuxInternals::polar4linuxInternals(QString &_application_path)
{
	IRDAok=0;
	application_path=_application_path;

	//hardcoded default values
	default_graph_configuration.colors.c0.setRgb(165,169,188,255);
	default_graph_configuration.colors.c1.setRgb(193,193,193,255);
	default_graph_configuration.colors.c2.setRgb(158,196,214,255);
	default_graph_configuration.colors.c3.setRgb(167,201,95,255);
	default_graph_configuration.colors.c4.setRgb(245,187,90,255);
	default_graph_configuration.colors.c5.setRgb(235,99,80,255);
	default_graph_configuration.colors.greyBG.setRgb(193,193,193,100);
	default_graph_configuration.colors.transpWhite.setRgb(255, 255, 255, 90);
	default_graph_configuration.colors.transpRed.setRgb(255,0,0,30);
	default_graph_configuration.colors.transpBlue.setRgb(0,0,255,90);
	
	hrm4preview.session=&s4preview;
	hrm4preview.pddContent=&p4preview;
	
	advancedexos.maximumNumberOfExercises=10;
	trainingexos.maximumNumberOfExercises=20;
}

polar4linuxInternals::~polar4linuxInternals()
{
}
